<%@ page language="java" pageEncoding="UTF-8"%>
<%@include file="/common/taglibs.jsp"%>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>添加目录</title>
		<link rel="stylesheet" type="text/css" href="${base }/css/caas_style.css" />
		<script type="text/javascript" src="${base }/js/jquery/jquery-1.3.min.js"></script>
		<script type="text/javascript" src="${base}js/jquery/lhgdialog/lhgcore.min.js"></script>
		<script type="text/javascript" src="${base}js/jquery/lhgdialog/lhgdialog.min.js"></script>
		<SCRIPT language=JavaScript>  
			jQuery(document).ready(function(){			
			    var dg = frameElement.lhgDG; 			   
			   			   
			    var param = dg.getArgs();
			    var pOrgCode = param.pOrgCode;
			    var operate = param.operate;
			    var dirId = param.dirId;
			    
			    if(operate == 'add'){
			    	dg.addBtn("btn_confirm","确定",addOrg,"left");
			    }else if(operate == 'update'){
			    	init(dirId);
			    	dg.addBtn("btn_confirm","确定",addOrg,"left");
			    }else if(operate == 'show'){
			    	init(dirId);
			    	jQuery("input").attr("disabled","disabled");
			    	jQuery("textarea").attr("disabled","disabled");			    	
			    }
			    
			    //确定按钮单击事件
				function addOrg(){				
					var url = operate == 'update'? "${base}/directory/update":"${base}/directory/add";
					var info = operate == 'update'?"修改":"添加";
					var param = {};
					var dirName = jQuery("#dirname").val();					
					var dirCode = jQuery("#dircode").val();
					var remark = jQuery("#remark").text();
					var flag = validate(dirName,dirCode,remark);
					if(!flag){
						return;
					}
					param.dirCode = dirCode;
					param.dirId = dirId;
					//检查目录编码是否已经存在
					jQuery.post("${base}/directory/directoryExists",param,function(data,textStatus,jqXHR){		 
						if(data.exists){
							alert("该目录编码已经存在，请重新输入!");
							jQuery("#dircode").focus();
							return;
						}else{					
							param = {};
							param["dir.dirName"] = dirName;
							param["dir.dirCode"] = dirCode;
							param["dir.remark"] = remark;
							param["dir.id"] = dirId;
						
							jQuery.post(url,param,function(data,textStatus,jqXHR){
								if(data.success){
									alert(info + "成功!");
									dg.curWin.refush();
								}else{
									alert(info + "失败!");
								}						
								dg.cancel();
							},'json');
						}						
					},'json');									
				}
				//end 确定按钮单击事件
				
				//初始化表单信息
				function init(_dirId){
					var param = {};
					param.dirId = _dirId;
					jQuery.post("${base}/directory/findDirectoryById",param,function(data,textStatus,jqXHR){
						jQuery("#dirname").val(data.dirName);					
						jQuery("#dircode").val(data.dirCode);
						jQuery("#remark").text(data.remark);						 
					},'json');									
				}				
				
				//检查页面元素输入的值是否合法
				function validate(orgName,orgCode,remark){
					if(orgName == null || orgName == ''){
						alert("目录名称不能为空!");
						jQuery("#dirname").focus();
						return false;
					}else if(orgName.length > 50){
						alert("目录名称不能超过50个字符!");
						jQuery("#dirname").focus();
						return false;
					}
					if(orgCode == null || orgCode == ''){
						alert("目录编码不能为空!");
						jQuery("#dircode").focus();
						return false;
					}else if(orgCode.length > 10){
						alert("目录编码不能超过10个字符!");
						jQuery("#dircode").focus();
						return false;
					}else if(!checkDirCode(orgCode)){
						alert("目录编码只能由字母和数字组成，且必须以字母开头!");
						jQuery("#dircode").focus();
						return false;
					}
					if(remark.length > 127){						
						alert("目录编码不能超过127个字符!");
						jQuery("#remark").focus();
						return false;
					}
					return true;
				}
				
				function checkDirCode(val){
					var regNum =/^[A-Za-z]+[A-Za-z0-9]*$/;
					return regNum.test(val)
				}
				//end 检查页面元素输入的值是否合法
			});
		</SCRIPT>
	</head>

	<body>
		<table width="99%" border="0" cellspacing="0" cellpadding="0"
			align="center">
			<tr>
				<td>
					<div class="biaoge">
						<form action="${ctx}/org/add" method="post" name="mainform" id="mainform">
							<table width="100%" border="0" cellspacing="1" cellpadding="0">
								<tr>
									<td align="left" class="biaoge_bg01" colspan="2">
										目录详细信息
									</td>
								</tr>
								<tr>
									<td width="30%" align="right" class="bg04">
										目录名称：
									</td>
									<td width="70%" class="biaoge_bg5">
										<label>
											<input type="text" name="dirname" id="dirname" size="50" /><font color="red" size="4">*</font>
										</label>
									</td>
								</tr>
								<tr>
									<td class="bg04" align="right">
										目录编码：
									</td>
									<td align="left" class="biaoge_bg5">
										<label>
											<input type="text" class="required" size="50" id="dircode"
												name="dircode" value=""><font color="red" size="4">*</font>
										</label>
									</td>
								</tr>
								<tr>
									<td class="bg04" align="right">
										备注信息：
									</td>
									<td align="left" class="biaoge_bg5">
										<label>
											<textarea rows="4" cols="39" name="remark" id="remark"></textarea>
										</label>
									</td>
								</tr>
							</table>
						</form>
					</div>
				</td>
			</tr>
		</table>
</html>